<?php
declare (strict_types=1);

namespace app\index\controller;

use app\common\model\Relation;
use think\exception\ValidateException;
use think\facade\View;
use app\common\model\User as UserModel;

class User extends Base
{

    public function index()
    {
        return View::fetch();
    }

    public function setting()
    {
        return View::fetch();
    }

    public function my_team()
    {
        if ($this->request->isAjax()) {
            $page = $this->request->param('page', 1);
            $map = ['uid' => $this->user->id, 'algebra' => 1];
            return json(Relation::getPageList($map, '*', $page, 20, false, ['fresh']));
        }
        $dpn = Relation::where(['uid' => $this->user->id, 'algebra' => 1])->column('new_uid');
        $team = Relation::where('uid', $this->user->id)->count();
        View::assign([
            'dpn' => count($dpn),
            'dpn_total' => UserModel::whereIn('id', $dpn)->sum('my_total'),
            'team' => $team,
            'team_total' => $this->user->team_total
        ]);
        return View::fetch();
    }

    public function method($type)
    {
        $mode = ['alipay' => '支付宝', 'wechat' => '微信', 'bank' => '银行卡'];
        View::assign([
            'type' => $type,
            'title' => $mode[$type],
            'bank_list' => explode(PHP_EOL, $this->system['bank_list'])
        ]);
        return View::fetch();
    }

    public function method_save($type)
    {
        try {
            $param = $this->request->post('', '', $this->filter);

            $v = $type == 'bank' ? 'MethodBank' : 'MethodImg';
            try {
                $this->validate($param, $v);
            } catch (ValidateException $e) {
                return json(['code' => -1, 'msg' => $e->getError()]);
            }
            switch ($type) {
                case "bank":
                    break;
                default:
                    $param[$type . '_name'] = $param['name'];
                    $param[$type . '_image'] = $param['image'];
                    unset($param['name']);
                    unset($param['image']);
            }

            UserModel::update($param, ['id' => $this->user->id]);

            return json(['code' => 1, 'msg' => '已保存', 'url' => $param['go_back'] ? url('draw/index')->build() : '']);

        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }

    public function edit()
    {
        try {
            $param = $this->request->post('', '', $this->filter);
            UserModel::update($param, ['id' => $this->user->id]);
            return json(['code' => 1, 'msg' => '已修改']);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }

    }

    public function logout()
    {
        $token = md5(time() . $this->user['id'] . $this->request->ip());

        UserModel::update([
            'token' => $token
        ], ['id' => $this->user['id']]);
        cookie('token', null);

        return json(['code' => 1, 'msg' => '已退出登录', 'url' => url('login/index')->build()]);
    }

}

